<?php
/**
 *
 * @author Tongle Xu <xutongle@gmail.com> 2013-11-18
 * @copyright Copyright (c) 2003-2103 tintsoft.com
 * @license http://www.tintsoft.com
 * @version $Id$
 */
namespace Leaps\Cache\Adapter;
use Leaps;
use Leaps\Filesystem\Filesystem;
use Leaps\Cache\AdapterInterface;
class File implements AdapterInterface
{

    /**
     * 文件系统实例
     *
     * @var \Leaps\Filesystem\Filesytem
     */
    protected $files;

    /**
     * 文件缓存目录
     *
     * @var string
     */
    protected $directory;

    /**
     * 创建一个新的文件存储实例
     *
     * @param \Leaps\Filesystem\Filesytem $files
     * @param string $directory
     * @return void
     */
    public function __construct(Filesystem $files, $directory)
    {
        $this->files = $files;
        $this->directory = Leaps::getAlias($directory);
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Leaps\Cache\StoreInterface::get()
     */
    public function get($key)
    {
        $cacheFile = $this->getCacheFile ( $key );
        if ( ! $this->files->exists ( $cacheFile ) ) {
            return null;
        }
        if ( $this->files->lastModified ( $cacheFile ) > time () ) {
            return $this->files->get ( $cacheFile );
        } else {
            return false;
        }
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Leaps\Cache\StoreInterface::set()
     */
    public function set($key, $value, $minutes)
    {
        $minutes = $this->expiration ( $minutes );
        $this->createCacheDirectory ( $cacheFile = $this->getCacheFile ( $key ) );
        $this->files->put ( $cacheFile, $value );
        $this->files->touch ( $cacheFile, $minutes );
    }

    /**
     * 创建缓存工作文件夹
     *
     * @param string $path
     * @return void
     */
    protected function createCacheDirectory($path)
    {
        if ( ! $this->files->isDirectory ( $directory = dirname ( $path ) ) ) {
            $this->files->createDirectory ( $directory, 0777, true );
        }
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Leaps\Cache\StoreInterface::forever()
     */
    public function forever($key, $value)
    {
        return $this->set ( $key, $value, 0 );
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Leaps\Cache\StoreInterface::delete()
     */
    public function delete($key)
    {
        $this->files->delete ( $this->getCacheFile ( $key ) );
    }

    /**
     * 删除所有缓存
     *
     * @return void
     */
    public function flush()
    {
        foreach ( $this->files->directories ( $this->directory ) as $directory ) {
            $this->files->deleteDirectory ( $directory );
        }
    }

    /**
     * 获取缓存文件路径
     *
     * @param string $key
     * @return string
     */
    protected function getCacheFile($key)
    {
        if ( strpos ( $key, '.' ) !== false ) {
            $key = str_replace ( '.', DIRECTORY_SEPARATOR, $key );
        }
        return $this->directory . DIRECTORY_SEPARATOR . $key;
    }

    /**
     * 获取过期时间
     *
     * @param int $minutes
     * @return int
     */
    protected function expiration($minutes)
    {
        if ( $minutes === 0 ) return 9999999999;
        return time () + ($minutes * 60);
    }

    /**
     * 获取文件驱动实例
     *
     * @return \Leaps\Filesystem\Filesystem
     */
    public function getFilesystem()
    {
        return $this->files;
    }

    /**
     * 获取缓存工作目录
     *
     * @return string
     */
    public function getDirectory()
    {
        return $this->directory;
    }
}